<?php namespace system\tag;

use hdphp\view\TagBase;

class Tag extends TagBase {
	/**
	 * 标签声明
	 * @var array
	 */
	public $tags
		= [
			'line' => [ 'block' => FALSE, 'level' => 4 ],
			'tag'  => [ 'block' => TRUE, 'level' => 4 ],
			'article'  => [ 'block' => TRUE, 'level' => 4 ],
		];

	public function _line( $attr ) {
		static $instance = [ ];
		$info   = explode( '.', $attr['action'] );
		$action = $info[1];
		if ( $module = Db::table( 'modules' )->where( 'name', $info[0] )->first() ) {
			$class = ( $module['is_system'] == 1 ? 'module' : 'addons' ) . '\\' . $info[0] . '\tag';
			if ( ! isset( $instance[ $class ] ) ) {
				$instance[ $class ] = new $class;
			}

			return $instance[ $class ]->$action( $attr );
		}
	}

	public function _tag( $attr, $content ) {
		static $instance = [ ];
		$info   = explode( '.', $attr['action'] );
		$action = $info[1];
		if ( $module = Db::table( 'modules' )->where( 'name', $info[0] )->first() ) {
			$class = ( $module['is_system'] == 1 ? 'module' : 'addons' ) . '\\' . $info[0] . '\tag';
			if ( ! isset( $instance[ $class ] ) ) {
				$instance[ $class ] = new $class;
			}

			return $instance[ $class ]->$action( $attr, $content );
		}
	}

	public function _article( $attr, $content ) {
		$row  = isset( $attr['row'] ) ? $attr['row'] : 10;
		$type = isset( $attr['type'] ) ? $attr['type'] : 'new';
		$php
		      = <<<str
		<?php
		\$db = Db::table('plugin_web_article')->limit($row);
		switch('$type'){
			case 'new':
				\$db->orderBy('id','DESC');
				break;
		}
		\$data = \$db->get();
		foreach((array)\$data as \$field):
			\$field['link']=web_url('site/show','web',['id'=>\$field['id']]);
		?>
		$content
str;
		$php .= "<?php endforeach;?>";

		return $php;
	}
}